<?php
class Warehouse_transfer_model extends CI_Model {

   
    private $_tabla1 = 'transfer';
    private $_tabla2 = 'transfer_details';

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

     public function search_transfer($document_code){

        
        $this->db->select();
        $this->db->where('document_code', ''.$document_code.'');
        $query = $this->db->get($this->_tabla1);
        $rs = $query->result();
        
        return $rs;
        
    }
    public function search_transfer_details($document_code){

        $query = $this->db->query("SELECT p.code,p.description, sd.box, sd.pack, sd.unit,sd.id_warehouse_o,sd.id_warehouse_d FROM products as p, transfer_details as sd where p.code = sd.product_code and sd.document_code = '".$document_code."'");
        $rs = $query->result();
        return $rs; 
        
    }
    
     public function search_transfer_details_by_product($document_code,$product_code){

        $query = $this->db->query("SELECT sd.box, sd.pack, sd.unit,sd.id_warehouse_o,sd.id_warehouse_d FROM transfer_details as sd where sd.product_code ='".$product_code."' and sd.document_code = '".$document_code."'");
        $rs = $query->result();
        return $rs; 
        
    }
    
     public function update_w_origen($code_product,$code_almacen,$caja_almacen,$paquete_almacen,$unidad_almacen)
    {
           $query = $this->db->query("UPDATE warehouses_product set units=units+'$unidad_almacen',packs =packs+'$paquete_almacen',boxes=boxes+'$caja_almacen' WHERE `id_product` = '$code_product' and `id_warehouse` = '$code_almacen'");
           $resultado = false;     
    }
    
     public function update_w_destino($code_product,$code_almacen,$caja_almacen,$paquete_almacen,$unidad_almacen)
    {
           $query = $this->db->query("UPDATE warehouses_product set units=units-'$unidad_almacen',packs =packs-'$paquete_almacen',boxes=boxes-'$caja_almacen' WHERE `id_product` = '$code_product' and `id_warehouse` = '$code_almacen'");
           $resultado = false;     
    }
    
     public function delete_detail($document_code, $code){
       return $this->db->query("DELETE FROM transfer_details WHERE  document_code='".$document_code."' AND product_code ='".$code."'");
    }
    
    public function update_transfer($id, $data)
        {
            
            $this->db->where('id_transfer', $id);
            $this->db->update($this->_tabla1, $data);
            return true;
        }
        
    public function comparaction_code($document_code){
        $query = $this->db->query("SELECT document_code FROM transfer where document_code = '".$document_code."'");
        $rs = $query->num_rows();
        return $rs;
    }
    
    public function add_transfer($data){
       return $this->db->insert($this->_tabla1, $data);
    }
    public function add_details_transfer($data){
       return $this->db->insert($this->_tabla2, $data);
    }
}